<template>
  <div>
    <van-nav-bar title="账号登入"> 
    <template #left>
    <van-icon name="arrow-left" size="18" color="white" @click="$router.go(-1)" />
  </template>
    </van-nav-bar>

    <van-form @submit="onSubmit" validate-trigger="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[
          { required: true, message: '请填写用户名' },
          { pattern: /^\w{5,}$/, message: '请输入5位字符以上的用户名' }
        ]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[
          { required: true, message: '请填写密码' },
          { pattern: /^\w{6,}$/, message: '请输入6位字符以上的用户名' }
        ]"
      />
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit" color="#23d881"
          >登入</van-button
        >
      </div>
    </van-form>

    <router-link to="/register" class="link">没有账号？去注册</router-link>
  </div>
</template>

<script>
import {login} from '@/api/user'
import {setToken} from '@/utils/storage'
export default {
  name: 'Login',
      data() {
    return {
      username: '',
      password: '',
    };
  },
  methods: {
  async  onSubmit(values) {
  const data=await login(values)
  if(data.data.status!==200) return this.$toast.fail(data.data.description)
   setToken(data.data.body.token)
  this.$toast.success('登入成功')
   this.$router.push('/')
  }
}
}
</script>

<style scoped lang="less">
.van-nav-bar {
  background-color: #23d881 !important;
}
.van-nav-bar .van-icon{
  color: white !important;
}
.link{
  display: flex;
  justify-content: center;
  color: #069;
  font-size: 12px;
}
</style>
